<?php

namespace longhu\library;

use EasyWeChat\OfficialAccount\Application;
use myshop\common\dao\ConfigValueDao;
use think\facade\Route;

class WechatService
{

    /**
     * @var Application
     */
    protected $application;

    protected $config;

    public function __construct(array $config)
    {
        $this->config = $config;
        $wecatService = new Application($config);
        $this->application = $wecatService;
    }
    /**
     * @return self
     * @author xaboy
     * @day 2020-04-24
     */
    public static function create($isApp = null)
    {
        return new self(self::getConfig($isApp));
    }


    public static function getConfig($isApp)
    {
        /** @var ConfigValueDao $make */
        $make = app()->make(ConfigValueDao::class);
        $wechat = $make->more([
            'wechat_appid', 'wechat_appsecret', 'wechat_token', 'wechat_encodingaeskey', 'wechat_encode', 'wecaht_app_appid', 'wechat_app_appsecret'
        ], 0);
       // dump($wechat);exit;
        $wechat['wechat_appid'] = trim($wechat['wecaht_app_appid']);
        $wechat['wechat_appsecret'] = trim($wechat['wechat_app_appsecret']);
        $payment = $make->more(['site_url', 'pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_v3_key', 'pay_weixin_open','pay_wechat_serial_no_v3', 'wechat_service_merid', 'wechat_service_key', 'wechat_service_v3key', 'wechat_service_client_cert', 'wechat_service_client_key', 'wechat_service_serial_no'], 0);
        $config = [
            'app_id' => trim($wechat['wechat_appid']),
            'secret' => trim($wechat['wechat_appsecret']),
            'token' => $wechat['wechat_token'],
          //  'routine_appId' => '123123',
            'http' => [
                'timeout' => 10.0, // 超时时间（秒）
                'retry' => true  //使用默认重试配置
            ],
            'use_stable_access_token' => false,
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'redirect_url' => 'http://test.huiyla.com/wechat/redirect',
            ],
        ];
        if ($wechat['wechat_encode'] > 0 && $wechat['wechat_encodingaeskey'])
            $config['aes_key'] = trim($wechat['wechat_encodingaeskey']);
       // $config['aes_key'] = 'I6N9yng3tSBiOVvOTvNqfYMlgUf25REMVJDpGnNkMGU';
       // $is_v3 = false;
        //是否开启微信支付
        if (isset($payment['pay_weixin_open']) && $payment['pay_weixin_open'] == 1) {
            $config['payment'] = [
                'merchant_id' => trim($payment['pay_weixin_mchid']),
                'key' => trim($payment['pay_weixin_key']),
                'apiv3_key' => trim($payment['pay_weixin_v3_key']),
                'serial_no' => trim($payment['pay_wechat_serial_no_v3']),
                'cert_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_cert']),
                'key_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_key']),
                'notify_url' => $payment['site_url'] . Route::buildUrl('wechatNotify')->build(),
                'pay_weixin_client_cert' => $payment['pay_weixin_client_cert'],
                'pay_weixin_client_key' => $payment['pay_weixin_client_key'],
            ];
            if ($config['payment']['apiv3_key']) {
                $is_v3 = true;
            }
        }
       // $config['is_v3'] = $is_v3;
        $config['service_payment'] = [
           // 'merchant_id' => trim($payment['wechat_service_merid']),
           // 'type' => 'wechat',
            //'key' => trim($payment['wechat_service_key']),
           // 'cert_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_cert']),
           // 'key_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_key']),
           // 'pay_weixin_client_cert' => $payment['wechat_service_client_cert'],
           // 'pay_weixin_client_key' => $payment['wechat_service_client_key'],
           // 'serial_no' => trim($payment['wechat_service_serial_no']),
           // 'apiv3_key' => trim($payment['wechat_service_v3key']),
        ];
       // dump($config);exit;
        return $config;
    }

    public function getApplication()
    {
        return $this->application;
    }
}